package com.winchain.chainbill.bill.biz.discount.rx;

import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctRevCallbackDctSubscriber extends OnErrorNotImplementedSubscriber<BillTranEvent<CbDct>> {
	@Autowired
	private CbDctService cbDctService;

	public void onApplicationEvent(BillTranEvent<CbDct> t) {
		List<Bill<CbDct>> child = (List<Bill<CbDct>>) t.getChildren();
		if (child.size() == 0) {
			Bill<CbDct> bill = t.getBill();
			CbDct cbDct = (CbDct) bill.getSource();
			optDct(cbDct);
		} else {
			for (Bill<CbDct> bill : child) {
				if (((CbDct) bill.getSource()).getDctId() != null) {
					CbDct cbDct = (CbDct) bill.getSource();
					optDct(cbDct);
				}
			}
		}
	}

	private void optDct(CbDct cbDct) {
		this.cbDctService.saveBillDctsByDctDate(cbDct, null, false);
	}
}
